<template>
  <div class="application">
    <div class="top flex-left">
      <div class="wallet-select flex-left" v-if="$store.state.userInfo.group === 'administrator'">
        <label>钱包</label>
        <el-select
          v-model="platform_id"
          placeholder="请选择"
          size="medium"
          @change="init(platform_id)"
        >
          <el-option
            v-for="item in $store.state.wallet_list"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>
      <div class="category-select flex-left">
        <label>类别</label>
        <el-select v-model="searchForm.id" placeholder="请选择" size="medium" @change="changeCategory">
          <el-option
            v-for="item in list"
            :key="item.id"
            :label="item.name['zh-CN']"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>
      <el-button type="primary" size="medium" @click="dialog.showSetDialog=true;dialog.type=0">新增应用</el-button>
    </div>
    <!-- 数据表 -->
    <div class="data-table">
      <el-table
        size="medium"
        v-loading="loading"
        element-loading-text="拼命加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(211, 215, 212, 0.5)"
        :data="tableData"
        stripe
        header-cell-class-name="headerStyle"
        style="width: 100%"
      >
        <el-table-column align="center" label="应用id" prop="id"></el-table-column>
        <el-table-column align="center" prop="name" label="应用名称">
          <template slot-scope="item">{{item.row.name['zh-CN']}}</template>
        </el-table-column>
        <el-table-column align="center" prop="sort" label="应用排序"></el-table-column>
        <el-table-column align="center" label="应用图标" width="140">
          <template slot-scope="item">
            <img :src="item.row.icon" class="scaleImg" @click="viewImg(item.row.icon)">
          </template>
        </el-table-column>
        <el-table-column align="center" label="应用状态">
          <template slot-scope="item">
            <span>{{item.row.status==='0'?'下线':'上线'}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="300">
          <template slot-scope="item">
            <el-button
              type="text"
              @click="upORdown(item,1)"
              v-if="Number(item.row.status)===0"
              :loading="onChange===item.$index"
            >上线</el-button>
            <el-button
              type="text"
              @click="upORdown(item,0)"
              v-if="Number(item.row.status)===1"
              :loading="onChange===item.$index"
            >下线</el-button>
            <el-button type="text" @click="edit(item.row)">修改</el-button>
            <el-button type="text" style="color:red" @click="del(item.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pages :pages="searchForm" :total="total" @pageChange="pageChange" layout="prev, pager, next"></pages>
      <application-dialog
        :visible="dialog"
        :category="list"
        :init="searchForm.id"
        @success="success()"
      ></application-dialog>
      <transition name="fade">
        <img-view :img="imgUrl" v-if="showImg" @cancel="showImg=false"></img-view>
      </transition>
    </div>
  </div>
</template>

<script>
import Pages from "@/components/PageBox.vue";
import ApplicationDialog from "@/components/Explorer/ApplicationSet.vue";
import ImgView from "@/components/Explorer/ImgView.vue";
import { getCategorys, getApps, delApp, updateApp } from "./Explorer.service";
export default {
  components: {
    Pages,
    ApplicationDialog,
    ImgView
  },
  data() {
    return {
      loading: false,
      tableData: [],
      list: [],
      total: 0,
      dialog: {
        showSetDialog: false,
        type: 0, //0->添加  1->編輯
        data: {}
      },
      searchForm: {
        page: 1,
        size: 10,
        title: "",
        id: "",
        platform_id: ""
      },
      showImg: false,
      imgUrl: "",
      onChange: "",
      platform_id: "1"
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    async init() {
      this.loading = true;
      if (this.$store.state.userInfo.group === "administrator") {
        this.searchForm.platform_id = this.platform_id;
      }
      const res = await getCategorys({ ...this.searchForm, ...{ size: 999 } });
      this.list = res.data.list || [];
      if (this.list.length > 0) {
        this.searchForm.id = this.list[0].id;
        this.search();
      } else {
        this.tableData = [];
        this.searchForm.id = "";
        this.loading = false;
      }
    },
    changeCategory(item) {
      this.searchForm.id = item;
      this.search();
    },
    async search() {
      this.loading = true;
      const res = await getApps(this.searchForm);
      this.tableData = res.data.list || [];
      this.total = res.data.page.pageCount * res.data.page.pageSize;
      this.loading = false;
      console.log(this.tableData);
    },
    del(item) {
      this.$confirm(
        `是否删除 <span class="confirm-tip-color">${
          item.name["zh-CN"]
        }</span> 应用?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          dangerouslyUseHTMLString: true
        }
      )
        .then(() => {
          delApp(item).then(res => {
            if (res.code === 0)
              this.$notify.success({
                title: "提示",
                message: "删除成功",
                duration: 1500
              });
            this.search();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    edit(item) {
      this.dialog = {
        showSetDialog: true,
        type: 1,
        data: item
      };
    },
    async upORdown(item, status) {
      this.onChange = item.$index;
      let param = JSON.parse(JSON.stringify(item.row));
      param["name[zh-CN]"] = param.name["zh-CN"];
      param["name[en-US]"] = param.name["en-US"];
      param["name[ja]"] = param.name["ja"];
      param.name = undefined;
      param.status = status;
      const res = await updateApp(param);
      if (res.code === 0) {
        this.$notify.success({
          title: "提示",
          message: status === 0 ? "应用下线成功" : "应用上线成功",
          duration: 1500
        });
      }
      this.search();
      this.onChange = "";
    },
    success() {
      this.$notify.success({
        title: "提示",
        message: this.dialog.type === 0 ? "上传成功" : "修改成功"
      });
      this.dialog = {
        showSetDialog: false,
        type: 0, //0->添加  1->編輯
        data: {}
      };
      this.search();
    },
    viewImg(url) {
      this.imgUrl = url;
      this.showImg = true;
    },
    pageChange(val) {
      this.searchForm.page = val;
      this.search(this.searchForm);
    }
  }
};
</script>

<style lang="scss">
.application {
  position: relative;
  min-height: calc(100vh - 100px);
  .top {
    .input-with-select {
      width: 341px;
      .el-input-group__append {
        background-color: #66b1ff;
        color: #fff;
      }
    }
    .category-select,
    .wallet-select {
      width: 230px;
      margin-right: 30px;
      color: #606266;
      label {
        width: 60px;
        flex-shrink: 1;
      }
    }
  }
  .data-table {
    position: relative;
    padding-bottom: 20px;
    background: #fff;
    border-radius: 5px;
    margin-top: 20px;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;
    .scaleImg {
      max-width: 120px;
      max-height: 23px;
      cursor: pointer;
    }
    .link {
      color: #409eff !important;
    }
  }
  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }
  .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
    opacity: 0;
  }
}
</style>